<template>
  <div>
    <a-card :bordered="false">
      <!-- 查询区域 -->
      <div class="table-page-search-wrapper">
        <a-form layout="inline" @keyup.enter.native="searchQuery">
          <a-row :gutter="24">
            <a-col :xl="6" :lg="7" :md="8" :sm="24">
              <a-form-item label="设备编码">
                <a-input placeholder="请输入设备编码" v-model="queryParam.code"></a-input>
              </a-form-item>
            </a-col>
            <a-col :xl="6" :lg="7" :md="8" :sm="24">
              <a-form-item label="设备名称">
                <a-input placeholder="请输入设备名称" v-model="queryParam.name"></a-input>
              </a-form-item>
            </a-col>
            <template v-if="toggleSearchStatus">
              <a-col :xl="6" :lg="7" :md="8" :sm="24">
                <a-form-item label="设备型号">
                  <a-input placeholder="请输入设备型号" v-model="queryParam.mode"></a-input>
                </a-form-item>
              </a-col>
              <a-col :xl="6" :lg="7" :md="8" :sm="24">
                <a-form-item label="设备类型">
                  <a-input placeholder="请输入设备类型" v-model="queryParam.categoryId"></a-input>
                </a-form-item>
              </a-col>
              <a-col :xl="6" :lg="7" :md="8" :sm="24">
                <a-form-item label="设备状态">
                  <j-dict-select-tag placeholder="请选择设备状态" v-model="queryParam.status"
                                     dict-code="equipment_status"></j-dict-select-tag>
                </a-form-item>
              </a-col>
              <a-col :xl="6" :lg="7" :md="8" :sm="24">
                <a-form-item label="检定单位">
                  <a-input placeholder="请输入检定单位" v-model="queryParam.verificationUnit"></a-input>
                </a-form-item>
              </a-col>
              <a-col :xl="6" :lg="7" :md="8" :sm="24">
                <a-form-item label="单位名称">
                  <a-input placeholder="请输入单位名称" v-model="queryParam.unitName"></a-input>
                </a-form-item>
              </a-col>
              <a-col :xl="6" :lg="7" :md="8" :sm="24">
                <a-form-item label="负责人">
                  <a-input placeholder="请输入负责人" v-model="queryParam.responsible"></a-input>
                </a-form-item>
              </a-col>
              <a-col :xl="6" :lg="7" :md="5" :sm="14">
                <a-form-item label="有效期">
                  <j-date v-model="queryParam.validityDate"></j-date>
                </a-form-item>
              </a-col>
            </template>
            <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <span style="float: left;overflow: hidden;" class="table-page-search-submitButtons">
              <a-button type="primary" @click="searchQuery" icon="search">查询</a-button>
              <a-button type="primary" @click="searchReset" icon="reload" style="margin-left: 8px">重置</a-button>
              <a @click="handleToggleSearch" style="margin-left: 8px">
                {{ toggleSearchStatus ? '收起' : '展开' }}
                <a-icon :type="toggleSearchStatus ? 'up' : 'down'"/>
              </a>
            </span>
            </a-col>
          </a-row>
        </a-form>
      </div>
      <!-- 查询区域-END -->

      <!-- 操作按钮区域 -->
      <div class="table-operator">
        <a-button @click="handleAdd" type="primary" icon="plus">新增</a-button>

        <a-dropdown v-if="selectedRowKeys.length > 0">
          <a-menu slot="overlay">
            <a-menu-item key="1" @click="batchDel">
              <a-icon type="delete"/>
              删除
            </a-menu-item>
            <a-menu-item key="2" @click="handleCheck(selectedRowKeys)">
              <a-icon type="file-text"/>
              检定记录
            </a-menu-item>
            <a-menu-item key="3" @click="handleRepair(selectedRowKeys)">
              <a-icon type="file-text"/>
              维修记录
            </a-menu-item>
            <a-menu-item key="4" @click="handleCheckAndRepair(selectedRowKeys)">
              <a-icon type="file-text"/>
              查看检定/维修记录
            </a-menu-item>
          </a-menu>
          <a-button style="margin-left: 8px"> 更多操作
            <a-icon type="down"/>
          </a-button>
        </a-dropdown>
      </div>

      <!-- table区域-begin -->
      <div>
        <div class="ant-alert ant-alert-info" style="margin-bottom: 16px;">
          <i class="anticon anticon-info-circle ant-alert-icon"></i> 已选择 <a style="font-weight: 600">{{
          selectedRowKeys.length }}</a>项
          <a style="margin-left: 24px" @click="onClearSelected">清空</a>
        </div>

        <a-table
          ref="table"
          size="middle"
          :scroll="{x:true}"
          bordered
          rowKey="id"
          :columns="columns"
          :dataSource="dataSource"
          :pagination="ipagination"
          :loading="loading"
          :rowSelection="{selectedRowKeys: selectedRowKeys, onChange: onSelectChange}"
          class="j-table-force-nowrap"
          @change="handleTableChange">

          <template slot="htmlSlot" slot-scope="text">
            <div v-html="text"></div>
          </template>
          <template slot="imgSlot" slot-scope="text">
            <span v-if="!text" style="font-size: 12px;font-style: italic;">无图片</span>
            <img v-else :src="getImgView(text)" height="25px" alt=""
                 style="max-width:80px;font-size: 12px;font-style: italic;"/>
          </template>
          <template slot="fileSlot" slot-scope="text">
            <span v-if="!text" style="font-size: 12px;font-style: italic;">无文件</span>
            <a-button
              v-else
              :ghost="true"
              type="primary"
              icon="download"
              size="small"
              @click="downloadFile(text)">
              下载
            </a-button>
          </template>

          <span slot="action" slot-scope="text, record">
          <a @click="handleEdit(record)">编辑</a>

          <a-divider type="vertical"/>
          <a-dropdown>
            <a class="ant-dropdown-link">更多 <a-icon type="down"/></a>
            <a-menu slot="overlay">
              <a-menu-item>
                <a @click="handleDetail(record)">详情</a>
              </a-menu-item>
              <a-menu-item>
                <a-popconfirm title="确定删除吗?" @confirm="() => handleDelete(record.id)">
                  <a>删除</a>
                </a-popconfirm>
              </a-menu-item>
            </a-menu>
          </a-dropdown>
        </span>

        </a-table>
      </div>

      <dev-equipment-modal ref="modalForm" @ok="modalFormOk"></dev-equipment-modal>
    </a-card>


    <!--新增检定记录弹窗-->
    <a-modal v-model="checkVisible" title="检定记录" @ok="addCheck" @cancel="cancelCheck">
      <a-form-model :model="check" :label-col="labelCol" :wrapper-col="wrapperCol"  ref="checkAddForm">
        <a-form-model-item label="设备编码:" prop="equipmentCode">
          <a-lable v-model="checkForm.equipmentCode" > {{departMent}}</a-lable>
        </a-form-model-item>
        <a-form-model-item label="检定部门:" prop="department">
          <a-input v-model="checkForm.department" />
        </a-form-model-item>
        <a-form-model-item label="检定人:" prop="person">
          <a-input v-model="checkForm.person" />
        </a-form-model-item>
        <a-form-model-item label="起始日期：" prop="startTime">
          <j-date v-model="checkForm.startTime" :trigger-change="true" allowClear/>
        </a-form-model-item>
        <a-form-model-item label="截止日期：" prop="endTime">
          <j-date v-model="checkForm.endTime" :trigger-change="true" allowClear/>
        </a-form-model-item>
        <a-form-model-item label="检定结果:" prop="result">
          <a-input v-model="checkForm.result" />
        </a-form-model-item>
        <a-form-model-item label="备注:" prop="description">
          <a-input v-model="checkForm.description" />
        </a-form-model-item>
      </a-form-model>
    </a-modal>

    <!--新增维修记录弹窗-->
    <a-modal v-model="repairVisible" title="维修记录" @ok="addRepair" @cancel="cancelCheck">
      <a-form-model :model="repair" :label-col="labelCol" :wrapper-col="wrapperCol"  ref="repairForm">
        <a-form-model-item label="设备编码:" prop="equipmentCode">
          <a-lable v-model="repairForm.equipmentCode" > {{departMent}}</a-lable>
        </a-form-model-item>
        <a-form-model-item label="维修间隔(天):" prop="repairDays">
          <a-input v-model="repairForm.repairDays" />
        </a-form-model-item>
        <a-form-model-item label="维修部门:" prop="department">
          <a-input v-model="repairForm.department" />
        </a-form-model-item>
        <a-form-model-item label="检查人员:" prop="checkPerson">
          <a-input v-model="repairForm.checkPerson" />
        </a-form-model-item>
        <a-form-model-item label="机修人员:" prop="maintainPerson">
          <a-input v-model="repairForm.maintainPerson" />
        </a-form-model-item>
        <a-form-model-item label="电工人员:" prop="electricianPerson">
          <a-input v-model="repairForm.electricianPerson" />
        </a-form-model-item>
        <a-form-model-item label="起始日期：" prop="repairStartTime">
          <j-date v-model="repairForm.repairStartTime" :trigger-change="true" allowClear/>
        </a-form-model-item>
        <a-form-model-item label="截止日期：" prop="repairEndTime">
          <j-date v-model="repairForm.repairEndTime" :trigger-change="true" allowClear/>
        </a-form-model-item>
        <a-form-model-item label="维修原因:" prop="reason">
          <a-input v-model="repairForm.reason" />
        </a-form-model-item>
        <a-form-model-item label="维修结果:" prop="result">
          <a-input v-model="repairForm.result" />
        </a-form-model-item>
        <a-form-model-item label="备注:" prop="description">
          <a-input v-model="repairForm.description" />
        </a-form-model-item>
      </a-form-model>
    </a-modal>


    <!--新增检定维修查看弹窗-->
    <a-modal v-model="checkAndRepairVisble" title="检定/维修记录" @ok="closeOpne" @cancel="closeOpne1">
      <a-row><a-lable>检定记录</a-lable></a-row>
      <a-table :data-source="checkdata" bordered>
        <a-table-column title="检定部门" data-index="department" width="80px" align="center"/>
        <a-table-column title="检定人" data-index="person" width="80px" align="center"/>
        <a-table-column title="检定日期" data-index="endTime"  align="center"width="100px"/>
        <a-table-column title="检定结果" data-index="result" align="center" width="80px"/>
        <a-table-column title="备注"  data-index="description" align="center" width="80px"/>
      </a-table>
      <a-row><a-lable>维修记录</a-lable></a-row>
      <a-table :data-source="repairdata" bordered>
        <a-table-column title="维修部门" data-index="department" width="60px"  align="center"/>
        <a-table-column title="机修人" data-index="maintainPerson" width="60px" align="center"/>
        <a-table-column title="电工" data-index="electricianPerson" width="60px" align="center"/>
        <a-table-column title="检查人" data-index="checkPerson" width="60px" align="center"/>
        <a-table-column title="维修日期" data-index="repairStartTime"  align="center"width="70px"/>
        <a-table-column title="间隔天数" data-index="repairDays" align="center" width="60px"/>
        <a-table-column title="维修原因"  data-index="reason" align="center" width="60px"/>
      </a-table>
    </a-modal>

  </div>
</template>

<script>


  import '@/assets/less/TableExpand.less'
  import {mixinDevice} from '@/utils/mixin'
  import {JeecgListMixin} from '@/mixins/JeecgListMixin'
  import DevEquipmentModal from './modules/DevEquipmentModal'
  import JDate from '@/components/jeecg/JDate.vue'
  import {filterDictTextByCache} from "../../../components/dict/JDictSelectUtil";
  import ACol from "ant-design-vue/es/grid/Col";
  import AFormItem from "ant-design-vue/es/form/FormItem";
  import ARow from "ant-design-vue/es/grid/Row";
  import { axios } from '@/utils/request'


  export default {
    name: 'DevEquipmentList',
    mixins: [JeecgListMixin, mixinDevice],
    components: {
      ARow,
      AFormItem,
      ACol,
      JDate,
      DevEquipmentModal
    },
    data() {
      return {

        repairVisible:false,
        repairdata:[],
        checkdata:[],
        checkAndRepairVisble:false,
        lastOne:"",
        check:{},
        repair:{},
        checkForm: {
          "equipmentCode": "",
          "id":"",
          "department":"",
         " person":"",
          "startTime":undefined,
          "endTime":undefined,
          "result":"",
          "description":""

        },
        repairForm:{
          "equipmentCode": "",
          "id":"",
          "department":"",
          "checkPerson":"",
          "repairStartTime":undefined,
          "repairEndTime":undefined,
          "result":"",
          "description":"",
          "repairDays":"",
          "maintainPerson":"",
          "electricianPerson":""

        },
        departMent:"",
        checkVisible: false,
        labelCol: {span: 4},
        wrapperCol: {span: 14},
        other: '',
        description: '设备台账表管理页面',
        // 表头
        columns: [
          {
            title: '#',
            dataIndex: '',
            key: 'rowIndex',
            width: 60,
            align: "center",
            customRender: function (t, r, index) {
              return parseInt(index) + 1;
            }
          },
          {
            title: '设备编码',
            align: "center",
            dataIndex: 'code'
          },
          {
            title: '设备名称',
            align: "center",
            dataIndex: 'name'
          },
          {
            title: '设备型号',
            align: "center",
            dataIndex: 'mode'
          },
          {
            title: '设备类型',
            align: "center",
            dataIndex: 'categoryId'
          },
          {
            title: '设备状态',
            align: "center",
            dataIndex: 'status',
            customRender: (text) => {

              return filterDictTextByCache('equipment_status', text);
            }
          },
          {
            title: '有效期',
            align: "center",
            dataIndex: 'validityDate',
            customRender: function (text) {
              return !text ? "" : (text.length > 10 ? text.substr(0, 10) : text)
            }
          },
          {
            title: '检定单位',
            align: "center",
            dataIndex: 'verificationUnit'
          },
          {
            title: '单位名称',
            align: "center",
            dataIndex: 'unitName'
          },
          {
            title: '负责人',
            align: "center",
            dataIndex: 'responsible'
          },

          {
            title: '描述',
            align: "center",
            dataIndex: 'description'
          },
          {
            title: '操作',
            dataIndex: 'action',
            align: "center",
            fixed: "right",
            width: 147,
            scopedSlots: {customRender: 'action'}
          }
        ],
        url: {
          list: "/dev/devEquipment/list",
          delete: "/dev/devEquipment/delete",
          deleteBatch: "/dev/devEquipment/deleteBatch",

        },
        dictOptions: {},
      }
    },
    created() {
    },
    computed: {
      importExcelUrl: function () {
        return `${window._CONFIG['domianURL']}/${this.url.importExcelUrl}`;
      },
    },
    methods: {
      initDictConfig() {
      },

      handleCheck(selectedRowKeys) {
        var id;
        id = selectedRowKeys[0];
        this.checkVisible = true;
        axios({
          url:'/dev/devEquipment/queryById/'+id,
          method:"get",

        }).then((resp) => {
            this.departMent= resp.result.code;
            this.checkForm.equipmentCode = this.departMent;
            this.getLastId();
        })

      },
      handleRepair(selectedRowKeys){
        var rid;
        rid = selectedRowKeys[0];
        this.repairVisible = true;
        axios({
          url:'/dev/devEquipment/queryById/'+rid,
          method:"get",
        }).then((resp) => {
          this.departMent= resp.result.code;
          this.repairForm.equipmentCode = this.departMent;
          this.getLastId1();
        })
      },
      addRepair(){
        this.repairForm.id = this.lastOne;
        this.repairVisible = false;
        this.selectedRowKeys=[];
        axios({
          data:this.repairForm,
          url:'/dev/devEquipmentRepair/add',
          method:'post',
        }).then((resp)=>{
          if(resp.success){
            alert("维修完成");
          }else {
            alert("维修失败");
          }
        })
      },
      addCheck(){
        this.checkForm.id = this.lastOne;
        this.checkVisible = false;
        this.selectedRowKeys=[];
        axios({
          data:this.checkForm,
          url:'/dev/devEquipmentVerification/add',
          method:'post',
        }).then((resp)=>{
          if(resp.success){
            alert("检定完成");
          }else {
            alert("检定失败");
          }
        })

      },
      cancelCheck(){
        this.selectedRowKeys=[];
      },

      //获取最后一个id，完成主键自增
      getLastId(){
        axios({
          url:'/dev/devEquipmentVerification/getLastId',
          method:"get",
        }).then((resp) => {
           this.lastOne = resp.result;
        })
      },
      getLastId1(){
        axios({
          url:'/dev/devEquipmentRepair/getLastId',
          method:"get",

        }).then((resp) => {
          this.lastOne = resp.result;
        })
      },

      handleCheckAndRepair(selectedRowKeys){
        var sid;
        var code1;
        sid = selectedRowKeys[0];
        this.checkAndRepairVisble = true;
        axios({
          url:'/dev/devEquipment/queryById/'+sid,
          method:"get",
        }).then((resp)=>{
          code1= resp.result.code;
          this.getCheck(code1);
          this.getRepair(code1);
        })

      },
      closeOpne(){
        this.checkAndRepairVisble = false;
        this.selectedRowKeys=[];
      },
      closeOpne1(){
        this.checkAndRepairVisble = false;
        this.selectedRowKeys=[];
      },
      getCheck(code){
        axios({
          url:'/dev/devEquipmentVerification/listByCode/'+code,
          method:"get",
        }).then((resp)=>{
          this.checkdata = resp.result;

        })
      },
      getRepair(code){
        axios({
          url:'/dev/devEquipmentRepair/listByCode/'+code,
          method:"get",
        }).then((resp)=>{
          this.repairdata = resp.result;
        })
      }
    }


  }
</script>
<style scoped>
  @import '~@assets/less/common.less';
</style>